﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Pages_Cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["KhachHang"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn chưa có giỏ hàng! Đăng nhập và mua hàng!');location.href='DangNhap.aspx';</script>");
        }
        if (!IsPostBack)
        {
            if (Session["CartOnline"] == null)
            {
                Session["CartOnline"] = new CartOnline();
            }
            CartOnline mycart = (CartOnline)Session["CartOnline"];
            mylist.DataSource = mycart.Table;
            mylist.DataBind();
            totalPrice.Text = string.Format("{0:#,##0}", mycart.TotalPrice);
        }
    }
    protected void btnBuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Session["CartOnline"] == null)
        {
            Session["CartOnline"] = new CartOnline();
        }
        CartOnline mycart = (CartOnline)Session["CartOnline"];

        for (int i = 0; i < mylist.Items.Count; i++)
        {
            RepeaterItem item = mylist.Items[i];
            CheckBox remove = (CheckBox)item.FindControl("Remove");
            HiddenField id = (HiddenField)item.FindControl("id");

            if (remove.Checked)
            {
                mycart.Remove(id.Value.ToString());

            }
            TextBox txtSoluong = (TextBox)item.FindControl("Soluong");
            mycart.Update(id.Value.ToString(), int.Parse(txtSoluong.Text));

        }

        mylist.DataSource = mycart.Table;
        mylist.DataBind();
        totalPrice.Text = string.Format("{0:#,##0}", mycart.TotalPrice);
    }
    protected void btnThanhtoan_Click(object sender, EventArgs e)
    {
        if (Session["KhachHang"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập để gửi đơn hàng/ You must login');location.href='Home.aspx';</script>");
        }
        else { 
            
        }
    }
}
